package c2.cross.business.ladder;

import c2.advanced.business.base.AdvancedDaoTemplate;
import c2.cross.business.ladder.common.LadderOppoDetail;
import c2.cross.business.ladder.common.LadderPVPRecord;
import c2.cross.share.DbFacade4Cross;
import com.mongodb.client.result.DeleteResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.query.Query;

import java.util.*;

@Slf4j
public class LadderDao extends AdvancedDaoTemplate {

    public LadderDao(DbFacade4Cross root) {
        super(root);
    }

    public List<LadderOppoDetail> getOppoRankList() {
        long beginTime = System.currentTimeMillis();
        List<LadderOppoDetail> list = this.mongo().findAll(LadderOppoDetail.class, LadderOppoDetail.COLLECTION);
        log.info("天梯赛排行榜全量拉取,耗时{}ms", (System.currentTimeMillis() - beginTime));
        return list != null ? list : new ArrayList<>(4);
    }

    public Collection<LadderOppoDetail> updateOppoRankList(List<LadderOppoDetail> list) {
        DeleteResult dr = this.mongo().remove(new Query(), LadderOppoDetail.COLLECTION);
        log.info("天梯赛排行榜更新list前先删除数据{}条, wasAcknowledged={}", dr.getDeletedCount(), dr.wasAcknowledged());
        if(list == null) {
            log.info("天梯赛排行榜试图insert null list");
            return null;
        }
        Collection<LadderOppoDetail> r = this.mongo().insert(list, LadderOppoDetail.COLLECTION);
        log.info("天梯赛排行榜试图insert {} 个数据, 实际插入 {} 个数据", list.size(), r.size());
        return r;
    }

    public List<LadderPVPRecord> getRecordsList() {
        long beginTime = System.currentTimeMillis();
        List<LadderPVPRecord> list = this.mongo().findAll(LadderPVPRecord.class, LadderPVPRecord.COLLECTION);
        log.info("天梯赛大神录像全量拉取,耗时{}ms", (System.currentTimeMillis() - beginTime));
        return list != null ? list : new ArrayList<>();
    }

    public Collection<LadderPVPRecord> updateRecordsList(List<LadderPVPRecord> records) {
        DeleteResult dr = this.mongo().remove(new Query(), LadderPVPRecord.COLLECTION);
        log.info("天梯赛大神风采更新list前先删除数据{}条, wasAcknowledged={}", dr.getDeletedCount(), dr.wasAcknowledged());
        if(records == null) {
            log.info("天梯赛大神风采试图insert null list");
            return null;
        }
        Collection<LadderPVPRecord> r = this.mongo().insert(records, LadderPVPRecord.COLLECTION);
        log.info("天梯赛大神风采试图insert {} 个数据, 实际插入 {} 个数据", records.size(), r.size());
        return r;
    }

    public void removeRecords() {
        DeleteResult dr = this.mongo().remove(new Query(), LadderPVPRecord.COLLECTION);
        log.info("天梯赛大神风采录像删除数据{}条, wasAcknowledged={}", dr.getDeletedCount(), dr.wasAcknowledged());
    }
}
